package com.neusoft.neuiotms.dcam.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.common.dto.ManufacturerDTO;
import com.neusoft.neuiotms.dcam.common.model.Manufacturer;
import com.neusoft.neuiotms.dcam.common.repository.ManufacturerRepository;
import com.neusoft.neuiotms.dcam.common.vo.ManufacturerImportVO;

@Component
public class ManufacturerConverter extends AbstractConverter<Manufacturer, ManufacturerDTO> {

    @Autowired
    private ManufacturerRepository manufacturerRepository;

    @Override
    public Manufacturer toModel(final ManufacturerDTO dto) {
        Manufacturer model;
        if (dto.isNew()) {
            model = new Manufacturer();
        } else {
            model = this.manufacturerRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setPerson(dto.getPerson());
        model.setPhone(dto.getPhone());
        model.setStatus(dto.getStatus());
        model.setAddress(dto.getAddress());
        model.setBank(dto.getBank());
        model.setAccountNumber(dto.getAccountNumber());
        model.setTaxId(dto.getTaxId());
        model.setPostCode(dto.getPostCode());

        return model;
    }

    @Override
    public ManufacturerDTO toDTO(final Manufacturer model, final boolean forListView) {
        final ManufacturerDTO dto = new ManufacturerDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setPerson(model.getPerson());
        dto.setPhone(model.getPhone());
        dto.setStatus(model.getStatus());
        dto.setAddress(model.getAddress());
        dto.setBank(model.getBank());
        dto.setAccountNumber(model.getAccountNumber());
        dto.setTaxId(model.getTaxId());
        dto.setPostCode(model.getPostCode());
        return dto;
    }

    public Manufacturer toModel(final ManufacturerImportVO vo) {
        final Manufacturer model = new Manufacturer();
        model.setName(vo.getName());
        model.setPerson(vo.getPerson());
        model.setPhone(vo.getPhone());
        model.setAddress(vo.getAddress());
        model.setBank(vo.getBank());
        model.setAccountNumber(vo.getAccountNumber());
        model.setTaxId(vo.getTaxId());
        model.setPostCode(vo.getPostCode());
        return model;
    }

}
